#include<iostream>
#include<Windows.h>
#include<malloc.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
using namespace std;
//*********************
void Process(char* buf, DWORD& w, DWORD& h)
{
    char* p;
    p = buf;
    //int r, g, b;
    for (int j = 0; j < h; j++)
    {
        for (int i = 0; i < w; i++)
        {
            *p = *p + 20; p++;
            *p = *p + 20; p++;
            *p = *p + 20; p++;
 
        }
    }
}
//*********************
void WriteImage(char* Name, char* buf, DWORD& w, DWORD& h, BITMAPFILEHEADER& bf, BITMAPINFOHEADER& bi)
{
    int r, g, b, pix;
    HWND wnd;                                 //窗口句柄
    HDC dc;
    char* p;
    FILE* fpw;
    wnd = GetForegroundWindow();               //获取窗口句柄
    dc = GetDC(wnd);                           //获取绘图设备
    int x = 40;
    int y = 40;
    p = buf;
    for (int j = 0; j < h; j++)
    {
        for (int i = 0; i < w; i++)
        {
            b = *p++; g = *p++; r = *p++;
            pix = RGB(r, g, b);
            SetPixel(dc, x + i, y + h - j, pix);
        }
    }
    fpw = fopen(Name, "wb");
    fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, fpw);  //写入文件头
    fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, fpw);  //写入文件头信息
    p = buf;
    for (int j = 0; j < h; j++)
    {
        for (int i = 0; i < w * 3; i++)
        {
            fwrite(p++, 1, 1, fpw);
        }
    }
    fclose(fpw);
}
void main()
{
 
    char fileName[20] = "1.bmp"; //定义打开图像名字
    char OutName[20] = "1_new.bmp";
    char* buf = nullptr;
    FILE* fp;                                 //定义文件指针
    DWORD w, h;                                //定义读取图像的长和宽
    DWORD bitCorlorUsed;                      //定义
    DWORD bitSize;                            //定义图像的大小
    BITMAPFILEHEADER bf;
    BITMAPINFOHEADER bi;
    if ((fp = fopen(fileName, "rb")) == NULL)
    {
        cout << "文件未找到！";
        exit(0);
    }
    fread(&bf, sizeof(BITMAPFILEHEADER), 1, fp);//读取BMP文件头文件
    fread(&bi, sizeof(BITMAPINFOHEADER), 1, fp);//读取BMP文件头文件信息
    w = bi.biWidth;                            //获取图像的宽
    h = bi.biHeight;                           //获取图像的高
    bitSize = bi.biSizeImage;                  //获取图像的size
    buf = (char*)malloc(w * h * 3);                //分配缓冲区大小
    fseek(fp, long(sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)), 0);//定位到像素起始位置
    fread(buf, 1, w * h * 3, fp); //开始读取数据
    fclose(fp);
    //************************
    Process(buf, w, h);
    //************************
    WriteImage(OutName, buf, w, h, bf, bi);
}